package datasource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/14.
 */
//创建数据库连接池，只需要创建一次，然后从每个里面取就可以了
public class C3P0Utils {
    //static代码块，是加载一次
    private  static  ComboPooledDataSource cdps=null;
    static {
        //C3P0的数据源对象
        cdps=new ComboPooledDataSource();
        try {
            cdps.setDriverClass("com.mysql.jdbc.Driver");
            cdps.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc");
            cdps.setUser("root");
            cdps.setPassword("root");
            cdps.setInitialPoolSize(5);
            cdps.setMaxPoolSize(15);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }

    }//static代码块
    public static Connection getConnettion() throws SQLException {
        return cdps.getConnection();
    }
}
